package com.minelx.scriptbuilder.script.attribute.value;

import com.minelx.scriptbuilder.script.IVariable;
import com.minelx.scriptbuilder.script.Variable;
import com.minelx.scriptbuilder.script.attribute.function.Params;

import java.util.HashMap;
import java.util.Map;

public class Attribute implements IAttribute {

	protected final Map<String, IAttribute> children;

	public Attribute() {
		children = new HashMap<>();
	}

	@Override
	public final void set(String name, IAttribute child) {
		children.put(name, child);
		// call it back to IAttribute.belongsTo
		child.belongsTo(this);
	}

	@Override
	public final Variable get(String name) {
		return new Variable(IVariable.immediate(this), name);
	}

	@Override
	public IAttribute value(String name) {
		return children.get(name);
	}

	@Override
	public String asString() {
		// FIXME 2020/6/18  wait for me!!!  attribute should be class
		IAttribute asString = value("asString");
		if (asString != null)
			return asString.execute(Params.empty()).asString();
		return "attribute";
	}

	@Override
	public String toString() {
		return "Attribute(" + children + ")";
	}
}
